package com.bit.springbook.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect // 表示是切面类
public class TimeRecordAspect {

    // 由于其他4种注解都可以被 @Around 替代 所以一般就不使用它们了
    @Around("execution(* com.bit.springbook.controller.*.*(..))")
    public Object recordTime(ProceedingJoinPoint joinPoint)  {

        Long start = System.currentTimeMillis();
        Object result = null;
        log.info("1. 执行指定函数前");
        try {
            log.info("2. 执行 Before");
            result = joinPoint.proceed();
            log.info("3. 执行 AfterReturning");
            log.info("4. 执行 After");
            log.info("5. 执行指定函数前后");
        } catch (Throwable t) {
            log.info("3. 执行 AfterThrowing");
            log.info("4. 执行 After");
        }

        log.info(joinPoint.getSignature() + "\n 耗时：" + (System.currentTimeMillis() - start));
        return result;
    }

//    @Before("execution(* com.bit.springbook.controller.*.*(..))")
//    public void before() throws Throwable {
//        log.info("2. 执行 Before");
//    }
//
//    @After("execution(* com.bit.springbook.controller.*.*(..))")
//    public void after() throws Throwable {
//        log.info("4. 执行 After");
//    }
//
//    @AfterReturning("execution(* com.bit.springbook.controller.*.*(..))")
//    public void afterReturning() throws Throwable {
//        log.info("3. 执行 AfterReturning");
//    }
//
//    @AfterThrowing("execution(* com.bit.springbook.controller.*.*(..))")
//    public void afterThrowing() throws Throwable {
//        log.info("3. 执行 AfterThrowing");
//    }

}
